/*
 * Copyright (c) 2017. 深圳掌控网络有限公司. All rights reserved.
 */

package com.yiqi.lottery.feature.login.presenter;

import android.text.TextUtils;

import com.suozhang.framework.entity.bo.UserInfoBo;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.utils.T;
import com.suozhang.framework.utils.ValidatorUtil;

import javax.inject.Inject;

import com.suozhang.framework.utils.logger.Logger;
import com.yiqi.lottery.entity.bo.RegisterBo;
import com.yiqi.lottery.feature.login.contract.RegisterContract;
import com.yiqi.lottery.feature.login.dependencies.register.DaggerRegisterPresenterComponent;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 *      LIJUWEN
 * @email yiyayiyayaoljw@gmail.com
 * @date 2017/4/11 9:31
 */
public class RegisterPresenter implements RegisterContract.Presenter {

    private RegisterContract.View mView;

    @Inject

    RegisterContract.Model mModel;


    public RegisterPresenter(RegisterContract.View mView) {
        this.mView = mView;
        DaggerRegisterPresenterComponent.create().inject(this);
    }

    @Override
    public void start() {

    }


    @Override
    public void doUserRegister(String mobile, String username, String code, String password, String invite_code) {
        if (TextUtils.isEmpty(username)) {
            T.showShort("用户名格式不正确！");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            T.showShort("密码不能为空！");
            return;
        }
        if (!ValidatorUtil.isMobile(mobile)) {
            T.showShort("手机号格式不正确！");
            return;
        }
        if (TextUtils.isEmpty(code)) {
            T.showShort("验证码不能为空！");
            return;
        }

        RegisterBo registerBo = new RegisterBo(mobile, username, code, password, invite_code);
        mModel.userRegister(registerBo).compose(mView.<UserInfoBo>bindToLife()).subscribe(new Observer<UserInfoBo>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(UserInfoBo s) {
                mView.dismissLoading();
                loginSuccess(s);
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showRegisterError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void doUserRegisterSendCode(String mobile) {
        if (!ValidatorUtil.isMobile(mobile)) {
            T.showShort("手机号格式不正确！");
            return;
        }
        mModel.userRegisterSendCode(mobile).compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(String s) {
                mView.showSendCodeSuccess(s);
                mView.dismissLoading();
            }

            @Override
            public void onError(Throwable e) {
                mView.showSendCodeError(e.getMessage());
                mView.dismissLoading();
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void doUserResetPwd(String mobile, String code, String password, String rePassword) {
        if (!ValidatorUtil.isMobile(mobile)) {
            T.showShort("手机号格式不正确！");
            return;
        }
        if (TextUtils.isEmpty(code)) {
            T.showShort("验证码不能为空！");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            T.showShort("新密码不能为空！");
            return;
        }
        if (TextUtils.isEmpty(rePassword)) {
            T.showShort("重复密码不能为空！");
            return;
        }
        if (!rePassword.equals(password)){
            T.showShort("两次密码输入不一致！");
            return;
        }

        RegisterBo registerBo = new RegisterBo(mobile, code,password);
        mModel.userResetPwd(registerBo).compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(String s) {
                mView.dismissLoading();
                mView.showResetSuccess(s);
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showResetError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }
    private void loginSuccess(UserInfoBo value) {
        Logger.d("登录成功 = " + value);
        AM.user().saveUserAccountCache(null);
        AM.user().saveLoginResultCache(value);
        mView.showRegisterSuccess();
    }

}
